'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 士兵
 * @author sundbcn@gmail.com (sun debing)
 */

var dataApi = require('../dataApi');
var constantUtil = require('../../util/constantUtil');
var soldierConf = require('../../config/soldierConf');
var weaknessAttr = require('./weaknessAttr');
var resistanceAttr = require('./resistanceAttr');
var attrFromAtkType = require('./attrFromAtkType');
var soldierProfesstionType = require('../../consts/soldierProfessionType');
var attrType = require('../../consts/attrType');
var equipPropertyService = require('../../services/equipPropertyService');

/**
 * 获取士兵相关的属性
 * @param  {[type]} soldier_type [description]
 * @param  {[type]} soldier_lv   [description]
 * @return {[type]}              [description]
 */
exports.getAttr = function (soldier_type, soldier_lv, soldier_star, talent_list) {
    var soldier_type_conf = dataApi.soldier_type.findById(soldier_type);
    if (!soldier_type_conf) {
        return {};
    }
    var soldier_star_conf = soldierConf.getSoldierStarConf(soldier_type, soldier_star);
    if (!soldier_star_conf) {
        return {};
    }
    var soldier_atk = soldier_star_conf.start_atk + soldier_lv * soldier_star_conf.atk_grow;
    var soldier_hp = soldier_star_conf.start_hp + soldier_lv * soldier_star_conf.hp_grow;
    var atk_type_attr = attrFromAtkType.getAttr(
        soldier_type_conf.atk_type,
        soldier_atk
    );

    var soldier_info = {
        soldier_type: soldier_type,
        command: soldier_type_conf.cost_space,
        produce_time: soldier_type_conf.produce_time,
        first_attack: soldier_type_conf.first_attack,
        star: soldier_star,
        attr: {
            aoe_radius: soldier_type_conf.aoe_radius,
            min_atk: atk_type_attr.min_atk || 0,
            // max_atk: atk_type_attr.max_atk || 0,
            // fire_damage: atk_type_attr.fire_damage || 0,
            // ice_damage: atk_type_attr.ice_damage || 0,
            // light_damage: atk_type_attr.light_damage || 0,
            // city_damage: atk_type_attr.city_damage || 0,
            // physical_def: 0,
            // fire_def: 0,
            // ice_def: 0,
            // light_def: 0,
            hp: soldier_hp,
            crit_addition: constantUtil.value('crit_addition'),
            atk_interval: soldier_type_conf.atk_interval,
            atk_range: soldier_type_conf.atk_range,
            warn_range: soldier_type_conf.warn_range,
            speed: soldier_type_conf.speed,
            atk_kind: soldier_type_conf.atk_kind,
            weakness_list: weaknessAttr.getAttr(soldier_type_conf.weakness_type),
            resistance_list: resistanceAttr.getAttr(soldier_type_conf.resistance_type),
            skill_ratio: soldier_type_conf.skill_ratio
        }
    };

    return _withTalentEff(soldier_info);
};

var talent_map = {};
talent_map[1] = 'crit';
talent_map[2] = 'crit_addition';
talent_map[3] = 'dodge';
talent_map[4] = 'uncrit';
talent_map[5] = 'uncrit_addition';
talent_map[6] = 'hp';
talent_map[7] = 'min_atk';
talent_map[8] = 'hit';
talent_map[9] = 'damage_sub';
talent_map[10] = 'ignore_def';

var _withTalentEff = function (soldier_info, talent_list) {
    if (!talent_list || talent_list.length !== 5) {
        return soldier_info;
    }
    for (var x = 0; x < 5; x++) {
        var ratio = soldierConf.getTalentRatio(soldier_info.star, x + 1);
        var talent_conf = soldierConf.getSoldierTalentLvConf(talent_list[x]);
        var field = talent_map[talent_conf.type];
        var talent_value = Math.floor(talent_conf.value * ratio / 100);
        var attr = soldier_info.attr;
        attr[field] = attr[field] || 0;
        if (talent_conf.value_type === 1) {
            attr[field] += talent_value;
        } else {
            attr[field] += Math.floor(attr[field] * talent_value / 100);
        }
    }
    return soldier_info;
};
